package com.RMP
{
    import flash.display.*;
    import flash.events.*;
    import com.greensock.*;

    public class BookmarksImgShell extends Sprite
    {
        var thumbnail:BookmarksThumbnail;
        public var myXMLList:XMLList;
        var counter:int;
        var myURL:String;
        var fadeSpeed:Number;
        var nav:BookmarksPaginatedMenu;
        var listLength:int;
        var currImageId:int;
        var imageCountArray:Array;
        var imageShell:BookmarksBigImageShell;
        var currNavId:int;
        public var hasSub:Boolean;
        var imageOrderString:String;
        var myRoot:MovieClip;
        var imageArray:Array;

        public function BookmarksImgShell(param1:MovieClip, param2:Number, param3:XMLList) : void
        {
            myRoot = param1;
            fadeSpeed = param2;
            myXMLList = param3;
            listLength = myXMLList.length();
            trace("myXMLList " + myXMLList);
            imageArray = new Array();
            imageCountArray = new Array();
            imageCountArray[0] = 0;
            counter = 0;
            currNavId = 0;
            buildImageGrid();
            nav = new BookmarksPaginatedMenu(myRoot, fadeSpeed, counter);
            nav.addEventListener(PaginatedMenuClick.MENU_CLICK, menuClick, false, 0, true);
            myRoot.stage.addEventListener(Event.RESIZE, onResizer, false, 0, true);
            nav.switchCols(currNavId);
            addChild(nav);
            updateGrid();
            imageShell = new BookmarksBigImageShell(myRoot, fadeSpeed, param3, 0);
            imageShell.addEventListener(ClickArrowEvent.CLICK_ARROW, arrowClick, false, 0, true);
            imageShell.addEventListener(ClickCloseEvent.CLICK_CLOSE, closeClick, false, 0, true);
            imageShell.addEventListener(ClickCloseEvent2.CLICK_CLOSE2, closeClick2, false, 0, true);
            imageShell.addEventListener(BookmarksDeleteBigImgEvent.BOOKMARKS_DELETE_BIG_IMG, deleteBigImg, false, 0, true);
            nav.x = Math.round(10 * (myRoot.unitW + myRoot.gutterW));
            nav.y = Math.round(myRoot.headerH + (myRoot.unitH + myRoot.gutterH) + -5 * myRoot.unitW / 75);
            return;
        }

        function arrowClick(event:ClickArrowEvent) : void
        {
            if (event.myDirection == "prev")
            {
                if (currImageId == 0)
                {
                    currImageId = imageArray.length - 1;
                }
                else
                {
                    (currImageId - 1);
                }
            }
            else if (currImageId == (imageArray.length - 1))
            {
                currImageId = 0;
            }
            else
            {
                (currImageId + 1);
            }
            imageShell.loadNextImage(imageArray[currImageId].myXMLList.@id);
            return;
        }

        function loadImage(param1:int) : void
        {
            currImageId = param1;
            addChild(imageShell);
            imageShell.fadeInShell();
            imageShell.loadImage(imageArray[currImageId].myXMLList.@id);
            return;
        }

        public function fadeOutShell() : void
        {
            TweenMax.to(this, fadeSpeed, {alpha:0, overwrite:true, onComplete:function ()
            {
                visible = false;
                return;
            }
            });
            return;
        }

        public function doResize() : void
        {
            var _loc_1:Number = NaN;
            var _loc_2:int = 0;
            var _loc_3:int = 0;
            var _loc_4:int = 0;
            var _loc_5:Number = NaN;
            var _loc_6:int = 0;
            _loc_1 = 0;
            _loc_2 = 0;
            _loc_3 = 0;
            nav.x = Math.round(10 * (myRoot.unitW + myRoot.gutterW));
            nav.y = Math.round(myRoot.headerH + (myRoot.unitH + myRoot.gutterH) + -5 * myRoot.unitW / 75);
            _loc_4 = imageArray.length;
            _loc_5 = Math.floor(_loc_4 / 8);
            _loc_6 = 0;
            while (_loc_6 < _loc_4)
            {
                
                imageArray[_loc_6].x = Math.round(_loc_1 * (myRoot.unitW + myRoot.gutterW));
                if (imageArray[_loc_6].myXMLList.thumbnail.@orient == "V")
                {
                    _loc_1 = _loc_1 + 1;
                    if (_loc_1 == 13)
                    {
                        _loc_1 = 1;
                        _loc_3++;
                        imageArray[_loc_6].x = 0;
                    }
                }
                else
                {
                    _loc_1 = _loc_1 + 2;
                    if (_loc_1 == 13 || _loc_1 == 14)
                    {
                        _loc_1 = 2;
                        _loc_3++;
                        imageArray[_loc_6].x = 0;
                    }
                }
                imageArray[_loc_6].y = Math.round(myRoot.headerH + 2 * (myRoot.unitH + myRoot.gutterH) + _loc_3 % 4 * (myRoot.unitH + myRoot.gutterH));
                _loc_6++;
            }
            return;
        }

        function deleteBigImg(event:BookmarksDeleteBigImgEvent)
        {
            deleteThumbnail(imageArray[currImageId].myId, imageArray[currImageId].myXMLList.@id, imageArray[currImageId].myXMLList.@parentId);
            return;
        }

        function onResizer(event:Event)
        {
            doResize();
            return;
        }

        function menuClick(param1:PaginatedMenuClick) : void
        {
            currNavId = param1.currentTarget.currNavId;
            updateGrid();
            return;
        }

        function closeClick(event:ClickCloseEvent) : void
        {
            imageShell.fadeOutShell();
            return;
        }

        function buildImageGrid() : void
        {
            var _loc_1:int = 0;
            var _loc_2:int = 0;
            var _loc_3:int = 0;
            counter = 1;
            _loc_1 = 0;
            _loc_2 = 0;
            _loc_3 = 0;
            while (_loc_3 < listLength)
            {
                
                imageArray[_loc_3] = new BookmarksThumbnail(myRoot, fadeSpeed, myXMLList[_loc_3], _loc_3, _loc_3);
                imageArray[_loc_3].buttonMode = true;
                imageArray[_loc_3].mouseChildren = true;
                imageArray[_loc_3].addEventListener(MouseEvent.CLICK, imageClick, false, 0, true);
                imageArray[_loc_3].addEventListener(MouseEvent.ROLL_OVER, imageRollOver, false, 0, true);
                imageArray[_loc_3].addEventListener(MouseEvent.ROLL_OUT, imageRollOut, false, 0, true);
                addChild(imageArray[_loc_3]);
                if (myXMLList[_loc_3].thumbnail.@orient == "V")
                {
                    _loc_1 = _loc_1 + 1;
                }
                else
                {
                    _loc_1 = _loc_1 + 2;
                }
                if (_loc_3 != (listLength - 1))
                {
                    if (_loc_1 == 11 && myXMLList[(_loc_3 + 1)].thumbnail.@orient != "V")
                    {
                        _loc_1 = 0;
                        _loc_2++;
                        if (_loc_2 == 4)
                        {
                            imageCountArray[counter] = _loc_3 + 1;
                            var _loc_5:* = counter + 1;
                            counter = _loc_5;
                            _loc_2 = 0;
                        }
                    }
                    if (_loc_1 == 12)
                    {
                        _loc_1 = 0;
                        _loc_2++;
                        if (_loc_2 == 4)
                        {
                            imageCountArray[counter] = _loc_3 + 1;
                            var _loc_5:* = counter + 1;
                            counter = _loc_5;
                            _loc_2 = 0;
                        }
                    }
                }
                _loc_3++;
            }
            return;
        }

        function updateGridNoFade() : void
        {
            var _loc_1:int = 0;
            doResize();
            _loc_1 = 0;
            while (_loc_1 < imageArray.length)
            {
                
                imageArray[_loc_1].myId = _loc_1;
                if (currNavId != (imageCountArray.length - 1))
                {
                    if (_loc_1 >= imageCountArray[currNavId] && _loc_1 < imageCountArray[(currNavId + 1)])
                    {
                        imageArray[_loc_1].alpha = 1;
                        imageArray[_loc_1].visible = true;
                    }
                    else
                    {
                        imageArray[_loc_1].alpha = 0;
                        imageArray[_loc_1].visible = false;
                    }
                }
                else if (_loc_1 >= imageCountArray[currNavId])
                {
                    imageArray[_loc_1].alpha = 1;
                    imageArray[_loc_1].visible = true;
                }
                else
                {
                    imageArray[_loc_1].alpha = 0;
                    imageArray[_loc_1].visible = false;
                }
                _loc_1++;
            }
            return;
        }

        function deleteThumbnail(param1, param2, param3) : void
        {
            var _loc_4:int = 0;
            trace("thumbnailId " + param1);
            myRoot.removeFromBookmarks(param3, param2);
            _loc_4 = 0;
            while (_loc_4 < imageArray.length)
            {
                
                if (imageArray[_loc_4].myId == param1)
                {
                    imageArray[_loc_4].removeEventListener(MouseEvent.CLICK, imageClick);
                    imageArray[_loc_4].removeEventListener(MouseEvent.ROLL_OVER, imageRollOver);
                    imageArray[_loc_4].removeEventListener(MouseEvent.ROLL_OUT, imageRollOut);
                    removeChild(imageArray[_loc_4]);
                    imageArray[_loc_4] = null;
                    imageArray.splice(_loc_4, 1);
                    break;
                }
                _loc_4++;
            }
            dispatchEvent(new BookmarksDeleteEvent(null, imageArray.length));
            reOrderImageGrid();
            updateGridNoFade();
            removeChild(nav);
            nav.removeEventListener(PaginatedMenuClick.MENU_CLICK, menuClick);
            nav = null;
            nav = new BookmarksPaginatedMenu(myRoot, fadeSpeed, counter);
            nav.addEventListener(PaginatedMenuClick.MENU_CLICK, menuClick, false, 0, true);
            if (currNavId > (nav.btnsArray.length - 1))
            {
                currNavId = nav.btnsArray.length - 1;
            }
            nav.switchCols(currNavId);
            if (nav.btnsArray.length > 1)
            {
                nav.alpha = 1;
            }
            addChild(nav);
            return;
        }

        function imageRollOver(event:MouseEvent) : void
        {
            event.currentTarget.rollOverEvent();
            return;
        }

        function reOrderImageGrid() : void
        {
            var _loc_1:int = 0;
            var _loc_2:int = 0;
            var _loc_3:int = 0;
            counter = 1;
            _loc_1 = 0;
            _loc_2 = 0;
            imageOrderString = "";
            _loc_3 = 0;
            while (_loc_3 < imageArray.length)
            {
                
                imageArray[_loc_3].myId = _loc_3;
                if (_loc_3 != (imageArray.length - 1))
                {
                    imageOrderString = imageOrderString + (imageArray[_loc_3].myXMLList.@parentId + ":" + imageArray[_loc_3].myXMLList.@id + ",");
                }
                else
                {
                    imageOrderString = imageOrderString + (imageArray[_loc_3].myXMLList.@parentId + ":" + imageArray[_loc_3].myXMLList.@id);
                }
                if (imageArray[_loc_3].myXMLList.thumbnail.@orient == "V")
                {
                    _loc_1 = _loc_1 + 1;
                }
                else
                {
                    _loc_1 = _loc_1 + 2;
                }
                if (_loc_3 != (imageArray.length - 1))
                {
                    if (_loc_1 == 11 && imageArray[(_loc_3 + 1)].myXMLList.thumbnail.@orient != "V")
                    {
                        _loc_1 = 0;
                        _loc_2++;
                        if (_loc_2 == 4)
                        {
                            imageCountArray[counter] = _loc_3 + 1;
                            var _loc_5:* = counter + 1;
                            counter = _loc_5;
                            _loc_2 = 0;
                        }
                    }
                    if (_loc_1 == 12)
                    {
                        _loc_1 = 0;
                        _loc_2++;
                        if (_loc_2 == 4)
                        {
                            imageCountArray[counter] = _loc_3 + 1;
                            var _loc_5:* = counter + 1;
                            counter = _loc_5;
                            _loc_2 = 0;
                        }
                    }
                }
                _loc_3++;
            }
            doResize();
            return;
        }

        function updateGrid() : void
        {
            var _loc_1:int = 0;
            doResize();
            _loc_1 = 0;
            while (_loc_1 < imageArray.length)
            {
                
                imageArray[_loc_1].myId = _loc_1;
                if (currNavId != (imageCountArray.length - 1))
                {
                    if (_loc_1 >= imageCountArray[currNavId] && _loc_1 < imageCountArray[(currNavId + 1)])
                    {
                        imageArray[_loc_1].fadeInShell();
                    }
                    else
                    {
                        imageArray[_loc_1].fadeOutShell();
                    }
                }
                else if (_loc_1 >= imageCountArray[currNavId])
                {
                    imageArray[_loc_1].fadeInShell();
                }
                else
                {
                    imageArray[_loc_1].fadeOutShell();
                }
                _loc_1++;
            }
            return;
        }

        function shiftThumbnailLeft(param1, param2, param3) : void
        {
            var _loc_4:* = undefined;
            _loc_4 = imageArray[param1];
            imageArray.splice(param1, 1);
            imageArray.splice((param1 - 1), 0, _loc_4);
            updateGridNoFade();
            reOrderImageGrid();
            myRoot.reorderBookmarks("images", imageOrderString);
            return;
        }

        function imageRollOut(event:MouseEvent) : void
        {
            event.currentTarget.rollOutEvent();
            return;
        }

        function shiftThumbnailRight(param1, param2, param3) : void
        {
            var _loc_4:* = undefined;
            _loc_4 = imageArray[param1];
            imageArray.splice(param1, 1);
            imageArray.splice((param1 + 1), 0, _loc_4);
            updateGridNoFade();
            reOrderImageGrid();
            myRoot.reorderBookmarks("images", imageOrderString);
            return;
        }

        function closeClick2(param1:ClickCloseEvent2) : void
        {
            imageShell.fadeOutShell2();
            return;
        }

        function imageClick(event:MouseEvent) : void
        {
            if (event.currentTarget.icons.hover == false)
            {
                loadImage(event.currentTarget.myId);
            }
            else if (event.currentTarget.icons.hoverMinus == true)
            {
                deleteThumbnail(event.currentTarget.myId, event.currentTarget.myXMLList.@id, event.currentTarget.myXMLList.@parentId);
            }
            else if (event.currentTarget.icons.hoverLeft == true)
            {
                if (imageArray[0].myId != event.currentTarget.myId)
                {
                    shiftThumbnailLeft(event.currentTarget.myId, event.currentTarget.myXMLList.@id, event.currentTarget.myXMLList.@parentId);
                }
                else
                {
                    trace("FIRST IN LINE");
                }
            }
            else if (event.currentTarget.icons.hoverRight == true)
            {
                if (imageArray[(imageArray.length - 1)].myId != event.currentTarget.myId)
                {
                    shiftThumbnailRight(event.currentTarget.myId, event.currentTarget.myXMLList.@id, event.currentTarget.myXMLList.@parentId);
                }
                else
                {
                    trace("Last LINE");
                }
            }
            return;
        }

        public function fadeInShell() : void
        {
            myRoot.menuShell.hRule3Off();
            visible = true;
            TweenMax.to(this, fadeSpeed, {alpha:1, delay:fadeSpeed, overwrite:true});
            return;
        }

    }
}
